﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Data.FileManager;
using System.IO;
using Logic.IB;
using Data.Probabilities;
using Data.DataTranslator;
using TreeView.Logic;
using System.Threading;
using Logic.Agglomerative;
using System.Collections;

namespace WindowsApplication2.Gui
{
    public partial class ShapedForm1 : Telerik.WinControls.UI.ShapedForm
    {
        private RadialTree originalTree;
        private ProbabilityData probData;
        public ShapedForm1()
        {
            InitializeComponent();
        }

        private void rbtn_run1_Click(object sender, EventArgs e)
        {
            //why we want to disable this button after loading ine file?
            //this.rbtn_run1.Enabled = false;
            FileHandler fh = FileHandler.Instance;
            StreamReader sReader = fh.openFileForReading("<Data>");
            kDimMap.probData = new ProbabilityData(sReader);
            StreamReader sReaderPropertiesCodes = fh.ValidateFile("<PropertiesData>", FileHandler.WorkingDirectory + "PropertiesData.dat");
            StreamReader sReaderPropertiesNames = fh.ValidateFile("<PropertiesNames>", FileHandler.WorkingDirectory + "PropertiesNames.dat");
            StreamReader sReaderGenomesNames = fh.ValidateFile("<GenomesNames>", FileHandler.WorkingDirectory + "GenomeNames.dat");
            Data.DataTranslator.kDimMap kdm = new Data.DataTranslator.kDimMap(sReaderPropertiesCodes, sReaderPropertiesNames, sReaderGenomesNames);
            DistanceFunction df = new DistanceFunction(kDimMap.probData);
            rtab_Tasks.Visibility = Telerik.WinControls.ElementVisibility.Visible;
            this.probData = kDimMap.probData;
        }

        private void rbtn_Run_Click(object sender, EventArgs e)
        {
            switch (rcmbx_Algorithm.SelectedIndex)
            {
                case 0://Agglomerative selected
                    ArrayList a = null;
                    rpbar_Algorithm.Value1 = 0;
                    kDimMap.aggProgBar = 0;
                    AgglomerativeData dataHold = new AgglomerativeData(probData);
                    Agglomerative t = new Agglomerative(dataHold, probData);
                    int mergType = rcmbx_MergType.SelectedIndex + 1;
                    int numOfCluster = (int)nud_ClusterNumber.Value;
                    Thread AggThread = new Thread(delegate()
                    {
                        a = t.runAgglomerative(mergType, numOfCluster);
                    });
                    AggThread.Start();
                    while (AggThread.IsAlive)
                    {
                        rpbar_Algorithm.Value1 = (int)kDimMap.aggProgBar;
                    }

                    // ArrayList a = t.runAgglomerative(mergType, numOfCluster,progressBar1);
                    double spreadAngle = (Double.Parse(txt_angle.Text)) / 180;
                    if (spreadAngle > 2)
                        spreadAngle = 2;
                    originalTree = new RadialTree(a, 2);
                    pnl_TreeResult1.EnableResultView("360", originalTree);
                    break;
                case 1://Sequential selected
                    rpnlbr_parameters.Visible = true;
                    pnl_AggP.Enabled = false;
                    pnl_SeqP.Enabled = true;
                    pnl_AggP.Expanded = false;
                    pnl_SeqP.Expanded = true;
                    nud_ClusterNumber.Value = 2;
                    break;
                case 2://Pam selected
                    rpnlbr_parameters.Visible = false;
                    break;
                case 3://Agglomerative and than Sequential selected
                    rpnlbr_parameters.Visible = true;
                    pnl_SeqP.Enabled = true;
                    pnl_AggP.Enabled = true;
                    pnl_AggP.Expanded = true;
                    pnl_SeqP.Expanded = false;
                    nud_ClusterNumber.Value = 2;
                    break;
            }
        }

        private void cmbx_Algorithm_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rcmbx_Algorithm.SelectedIndex)
            {
                case 0://Agglomerative selected
                    rpnlbr_parameters.Visible = true;
                    pnl_SeqP.Enabled = false;
                    pnl_AggP.Enabled = true;
                    pnl_AggP.Expanded = true;
                    pnl_SeqP.Expanded = false;
                    nud_ClusterNumber.Value = 1;
                    break;
                case 1://Sequential selected
                    rpnlbr_parameters.Visible = true;
                    pnl_AggP.Enabled = false;
                    pnl_SeqP.Enabled = true;
                    pnl_AggP.Expanded = false;
                    pnl_SeqP.Expanded = true;
                    nud_ClusterNumber.Value = 2;
                    break;
                case 2://Pam selected
                    rpnlbr_parameters.Visible = false;
                    break;
                case 3://Agglomerative and than Sequential selected
                    rpnlbr_parameters.Visible = true;
                    pnl_SeqP.Enabled = true;
                    pnl_AggP.Enabled = true;
                    pnl_AggP.Expanded = true;
                    pnl_SeqP.Expanded = false;
                    nud_ClusterNumber.Value = 2;
                    break;
            }
        }

        private void ShapedForm1_Load(object sender, EventArgs e)
        {
            rcmbx_Algorithm.SelectedIndex = 0;
            rcmbx_MergType.SelectedIndex = 0;
        }

        
    }
}